package com.qf.mybatis01.entity;

import cn.hutool.core.util.RandomUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Book {

    private Integer id;

    private String name;

    private String author;

    private String publish;

    private Integer sort;


    /**
     * 随机生成一本书的静态方法
     * @return
     */
    public static Book randomBook(){

        // 利用hutool工具类生成随机的字符串和 int
        String name = RandomUtil.randomString(20);
        String author = RandomUtil.randomString(10);
        String publish = RandomUtil.randomString(20);

        Integer sort = RandomUtil.randomInt(1,500000);

        return new Book(null, name, author, publish, sort);

    }


    /**
     * 生成随机的 book 的list
     * @param size list 的大小
     * @return
     */
    public static List<Book> randomBookList(Integer size){

        final ArrayList<Book> books = new ArrayList<>();

        for (int i = 0; i < size; i++) {
            books.add(randomBook());
        }

        return books;

    }


    /**
     * List<Book>  -----> List<String>
     * String 的格式为 csv格式 ， csv格式是指逗号分隔的文本文件 ，  aaa,bbb,ccc
     * @param
     */
    public static List<String> randomBookCSVList(Integer size){

        final ArrayList<String> books = new ArrayList<>();

        for (int i = 0; i < size; i++) {
            books.add(randomBook().toCSVString());
        }

        return books;

    }

    /**
     * cvs字符串 ---->  book对象
     * @param s
     * @return
     */
    public static Book cvsToBook(String s) {

        final String[] split = s.split(",");
        String name = split[0];
        String author = split[1];
        String publish = split[2];

        Integer sort = Integer.parseInt(split[3]);

        return new Book(null, name, author, publish, sort);
    }

    /**
     * 把 book 对象转换成 csv字符串
     * @return
     */
    public String toCSVString(){
        String s = this.getName()+","+this.getAuthor()+","+this.getPublish()+","+this.getSort();
        return s;
    }


    public static void main(String[] args) {
//        System.out.println(Book.randomBook());
        final List<Book> books = Book.randomBookList(10);
        books.forEach(System.out::println);
    }

}
